<?php

namespace App\Controllers\Admin;
use Pub;
use Sentry,User,DB,Template,Work,Cache,Wechat,Service;
use App,Notification;
use BaseController,View,Input,Form,Order,Customer,Redirect;
class WorksController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 * GET /admin/works
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        $user = Sentry::getUser();
        $METHOD = 'works';
        $ACTION = 'index';
        $works = Work::where('section_id',$user->area)->orderBy('created_at', 'DESC')->paginate(20);
        return View::make('admin.works.index',compact('METHOD','ACTION','works'));
	}

	/**
	 * Show the form for creating a new resource.
	 * GET /admin/works/create
	 *
	 * @return Response
	 */
	public function create()
	{
		//
        $user = Sentry::getUser();
        $METHOD = 'works';
        $ACTION = 'index';;
        return View::make('admin.works.create',compact('METHOD','ACTION'));
	}

	/**
	 * Store a newly created resource in storage.
	 * POST /admin/works
	 *
	 * @return Response
	 */
	public function store()
	{
		//
        $user = Sentry::getUser();
        Work::addWork(Input::get('name'),Input::get('mobile'),$user->area,$user->city);
        return Redirect::route('admin.works.index');
	}

	/**
	 * Display the specified resource.
	 * GET /admin/works/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 * GET /admin/works/{id}/edit
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 * PUT /admin/works/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 * DELETE /admin/works/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

    public function export(){
        $id = Input::get('id');
        $work = Work::find($id);
        $services = Service::where('worker_id',$id)->get();
        $this->_excel($services,$work);
    }




    protected function _excel($lists,$work){

        require app_path().'/lib/Excel/PHPExcel.php';
        $objPHPExcel=new \PHPExcel();
        $objPHPExcel->getProperties()->setCreator('http://www.jb51.net')
            ->setLastModifiedBy('http://www.jb51.net')
            ->setTitle('Office 2007 XLSX Document')
            ->setSubject('Office 2007 XLSX Document')
            ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.')
            ->setKeywords('office 2007 openxml php')
            ->setCategory('Result file');
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1','客户')
            ->setCellValue('B1','联系方式')
            ->setCellValue('C1','服务类型')
            ->setCellValue('D1','服务时间')
            ->setCellValue('E1','状态')
            ->setCellValue('F1','客户评价');

//        var_dump($list);
        $i = 2;
        foreach($lists as $service){


            if($service->type == 1):
                $type ='试用';
            elseif($service->type == 10):
                $type = '订购（付款方式：';
                if($service->paytype == 1):
                    $type .='付现金';
                elseif($service->paytype == 2):
                    $type = '微信支付';
                    if($service->all == 1):
                        $type .= '-已完成';
                    else:
                        $type .= '-未完成';
                    endif;
                endif;
                $type .= ')';
            elseif($service->type == 11):
                $type = '拆机';
            elseif($service->type == 12):
                $type = '维修';
            elseif($service->type == 13):
                $type =  '换芯-'.$service->commodity->name.'';
            endif;




            if($service->state== 1):
                $state = '尚未派单';
            elseif($service->state == 2):
                $state =   $work->name.'负责上门';
            elseif($service->state == 3):
                $state =   $work->name.'已完成';
            endif;




            if($service->rate_id== 0):
                $rate =  '尚未评价';
            else:
               if($service->rate->form1 >3):
                   $rate = '好评';
               elseif($service->rate->form1 < 1):
                   $rate = '差评';
               else:
                   $rate = '中评';
               endif;
            endif;



            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValueExplicit('A'.$i,$service->mount->mount_name,\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValueExplicit('B'.$i,$service->mount->mount_mobile,\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue('C'.$i,$type)
                ->setCellValue('D'.$i,$service->remark)
                ->setCellValueExplicit('E'.$i,$state,\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue('F'.$i,$rate);
            $i++;
        }

        $objPHPExcel->getActiveSheet()->setTitle('Sheet1');
        $objPHPExcel->setActiveSheetIndex(0);
        $filename=urlencode($work->name.'服务清单');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');


        $objWriter->save('php://output');

    }




    public function deleteit(){
        $id = Input::get('delete_id');
        $work = Work::find($id);

        $work->delete();

        return Redirect::route('admin.works.index');
    }
    public function er(){
        $data = Template::send('oeC5Ls08IpcOFSN7WBEZnk8yiUOs');
        var_dump($data);
        exit;
        $METHOD = 'works';
        $ACTION = 'er';
        return View::make('admin.works.er',compact('METHOD','ACTION'));
    }


    public function get_code(){
        $pub = Pub::find(1);
        if (Cache::has('token'))
        {
            //
            $value = Cache::get('token');
        }else{
            $value =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $value, 240);
        }
        $id = Input::get('id');
        $id = (int)$id;
        $true = 99999999 - $id;
        $arr['ticket'] = Wechat::get_pic($value,$true,1,true);
        if($arr['ticket'] == 'error'){
            $value =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $value, 240);
            $arr['ticket'] = Wechat::get_pic($value,$true,1,true);
        }
        echo '<img src="'.$arr['ticket'].'">';
    }

    public function set_paidan(){
        $id = Input::get('id');
        $work = Work::find($id);

        DB::table('works')
            ->where('section_id', $work->section_id)
            ->update(array('paidan' => 0));

        $work = Work::find($id);
        $work->paidan = 1;
        $work->save();
    }

}